﻿#include "NixieTube.h"
#include <QFile>

NixieTube::NixieTube(QWidget *parent)
    : QWidget(parent)
    , m_iCurrent_Num(0)
    , m_iMin_Num(0)
    , m_iMax_Num(9)
{
    InitUi();

    InitProperty();
}

NixieTube::~NixieTube()
{

}

void NixieTube::SetCurrentNum(int num)
{
    m_iCurrent_Num = num;
    m_pNumLabel->setText(QString::number(m_iCurrent_Num));
}

int NixieTube::GetCurrentNum() const
{
    return m_iCurrent_Num;
}

void NixieTube::SetMinNum(int num)
{
    m_iMin_Num = num;
}

void NixieTube::SetMaxNum(int num)
{
    m_iMax_Num = num;
}

void NixieTube::SetButtonVisible(bool state)
{
    m_pAddButton->setVisible(state);
    m_pSubtractButton->setVisible(state);
}

void NixieTube::InitUi()
{
    m_pRootLayout = new QVBoxLayout(this);
    m_pRootLayout->setContentsMargins(0,0,0,0);
    m_pRootLayout->setSpacing(0);

    m_pAddButton = new QPushButton(this);
    m_pAddButton->setObjectName("AddButton");
    m_pAddButton->setText("+");

    m_pNumLabel = new QLabel(this);
    m_pNumLabel->setObjectName("NumLabel");
    m_pNumLabel->setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);

    m_pSubtractButton = new QPushButton(this);
    m_pSubtractButton->setObjectName("SubtractButton");
    m_pSubtractButton->setText("-");

    m_pRootLayout->addWidget(m_pAddButton);
    m_pRootLayout->addWidget(m_pNumLabel);
    m_pRootLayout->addWidget(m_pSubtractButton);
}

void NixieTube::InitProperty()
{
    this->setWindowFlags(Qt::FramelessWindowHint);
    setAutoFillBackground(true);
    QPalette pal = palette();
    pal.setColor(QPalette::Background,Qt::transparent);
    setPalette(pal);
    setWindowOpacity(1);
    setAttribute(Qt::WA_TranslucentBackground);

    //this->setMinimumSize(QSize(500, 300));

    QFile resourceqss(":/qss/NixieTube.qss");
    resourceqss.open(QFile::ReadOnly);
    this->setStyleSheet(resourceqss.readAll());
    resourceqss.close();

    connect(m_pAddButton, &QPushButton::clicked, this, [=](){
        if(m_iCurrent_Num < m_iMax_Num)
        {
            m_iCurrent_Num++;
            m_pNumLabel->setText(QString::number(m_iCurrent_Num));
            emit currentnumed(m_iCurrent_Num);
        }
    });

    connect(m_pSubtractButton, &QPushButton::clicked, this, [=](){
        if(m_iCurrent_Num > m_iMin_Num)
        {
            m_iCurrent_Num--;
            m_pNumLabel->setText(QString::number(m_iCurrent_Num));
            emit currentnumed(m_iCurrent_Num);
        }
    });

    m_pNumLabel->setText(QString::number(m_iCurrent_Num));

}

void NixieTube::on_reset()
{
    m_iCurrent_Num = 0;
    m_pNumLabel->setText(QString::number(m_iCurrent_Num));
}
